﻿using System.Collections.Generic;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvUtilities;
using MyFloridaHouse.libSvOpenDistricts.Census;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using System.Collections.ObjectModel;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.StorageIsolated
{
    public static class IsolatedStorageHelper
    {
        #region Census Geometry

        internal static DateTime GetCensusGeometryFileDate()
        {
            return IsolatedStorageManager.GetFileCreationTime(Constants.IsolatedSaveDirectoryName, Constants.IsolatedCensusGeometryFileName);
        }

        internal static bool SaveCensusGeometry(List<CensusGeometryNode> saveList)
        {
            byte[] serializedData = SerializationManager.Serialize(saveList);
            byte[] serializedAndCompressedData = CompressionManager.Compress(serializedData);
            return IsolatedStorageManager.SaveData(serializedAndCompressedData, Constants.IsolatedSaveDirectoryName, Constants.IsolatedCensusGeometryFileName);
        }


        internal static List<CensusGeometryNode> LoadCensusGeometry()
        {
            byte[] serializedAndCompressedData = IsolatedStorageManager.LoadBinaryData(Constants.IsolatedSaveDirectoryName, Constants.IsolatedCensusGeometryFileName);
            byte[] serializedData = CompressionManager.DecompressToByteArray(serializedAndCompressedData);
            return (List<CensusGeometryNode>)SerializationManager.Deserialize(typeof(List<CensusGeometryNode>), serializedData);
        }

        internal static void DeleteCensusGeometry()
        {
            IsolatedStorageManager.Deletefile(Constants.IsolatedSaveDirectoryName, Constants.IsolatedCensusGeometryFileName);
            return;
        }

        #endregion

        #region DistrictPlanTypes

        internal static DateTime GetDistrictPlanTypesFileDate()
        {
            return IsolatedStorageManager.GetFileCreationTime(Constants.IsolatedSaveDirectoryName, Constants.IsolatedDistrictPlanTypesFileName);
        }

        internal static bool SaveDistrictPlanTypes(ObservableCollection<DistrictPlanTypeDTO> saveList)
        {
            byte[] serializedData = SerializationManager.Serialize(saveList);
            byte[] serializedAndCompressedData = CompressionManager.Compress(serializedData);
            return IsolatedStorageManager.SaveData(serializedAndCompressedData, Constants.IsolatedSaveDirectoryName, Constants.IsolatedDistrictPlanTypesFileName);
        }

        internal static ObservableCollection<DistrictPlanTypeDTO> LoadDistrictPlanTypes()
        {
            byte[] serializedAndCompressedData = IsolatedStorageManager.LoadBinaryData(Constants.IsolatedSaveDirectoryName, Constants.IsolatedDistrictPlanTypesFileName);
            byte[] serializedData = CompressionManager.DecompressToByteArray(serializedAndCompressedData);
            return (ObservableCollection<DistrictPlanTypeDTO>)SerializationManager.Deserialize(typeof(ObservableCollection<DistrictPlanTypeDTO>), serializedData);
        }

        internal static void DeleteDistrictPlanTypes()
        {
            IsolatedStorageManager.Deletefile(Constants.IsolatedSaveDirectoryName, Constants.IsolatedDistrictPlanTypesFileName);
            return;
        }
        #endregion
    }
}
